package com.hzit.aspect;

import cn.hutool.jwt.JWTUtil;
import com.alibaba.fastjson.JSON;
import com.hzit.annotation.RequiresPermissions;
import com.hzit.entity.LoginUser;
import com.hzit.enums.Logical;
import com.hzit.excep.NotPermissionException;
import com.hzit.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 作者：李嘉浩
 * 时间：2024-09-01-17:57
 * 功能：
 */

@Aspect
@Component
public class PreAuthAspect {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Around("@annotation(permissions)")
    public Object innerAround(ProceedingJoinPoint point, RequiresPermissions permissions) throws Throwable {
        //获得所需权限
        String[] permission = permissions.value();
        //获得权限形式
        Logical logical = permissions.logical();
        //过的用户有的权限
        List<String> permissionList= getPermissionByToken();
        //判断是否有权限
        isExistPermission(permission,logical,permissionList);
        return point.proceed();
    }

    /**
     * 判断是否有权限
     * @param permission    所需权限
     * @param logical       所需权限类型
     * @param permissionList    拥有的权限列表
     */
    private void isExistPermission(String[] permission, Logical logical, List<String> permissionList) {
        if (permission==null){
            return;
        }
        boolean flag=false;
        for (String per:permission){
            if (logical==Logical.AND){
                flag=true;
                if (!permissionList.contains(per)){
                    throw new NotPermissionException("没有权限");
                }
            }
            if (logical==Logical.AND){
                if (permissionList.contains(per)){
                    flag=true;
                }
            }
        }
        if (flag){
            throw new NotPermissionException("没有权限");
        }

    }

    /**
     * 获取用户权限
     * @return
     */
    private List<String> getPermissionByToken() {
        //获得请求对象
        ServletRequestAttributes servletRequestAttributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String authorization = request.getHeader("Authorization");
        String token = authorization.replace("Bearer ", "");
        Claims claims = JwtUtils.parseToken(token);
        Object uuid = claims.get("uuid");
        LoginUser loginUser = JSON.parseObject(stringRedisTemplate.opsForValue().get("access_token" + uuid), LoginUser.class);

        return loginUser.getPermissions();
    }
}
